package com.woniu.cloud.shopassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TGood;
import com.woniu.cloud.shopassembly.mapper.TGoodMapper;
import com.woniu.cloud.shopassembly.service.ITGoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 平台对店铺商品表 服务实现类
 * </p>
 *
 * @author hyy
 * @since 2021-09-22
 */
@Service
@Slf4j
public class TGoodServiceImpl extends ServiceImpl<TGoodMapper, TGood> implements ITGoodService {



    @Resource
    private ITGoodService goodService;

    @Resource
    private TGoodMapper goodMapper;

    public TGood updateRedis(Integer goodId) {
        log.info("缓存修改->id:{}",goodId);
        TGood good = goodMapper.selectById(goodId);
        log.info("修改结果->{}",good);
        return good;
    }
    @Override
    public TGood insertRedis(Integer goodId) {
        TGood good=new TGood();
            log.info("缓存添加->id:{}",goodId);
            good = goodMapper.selectById(goodId);
            log.info("缓存结果->{}",good);
        return good;
    }

    @Override
    public TGood getGoodByGoodId(Integer goodId) {
        log.info("正在查询->id:{}",goodId);
        TGood good = goodService.insertRedis(goodId);
        return good;
    }

    @Override
    public Result getGoodByGoodName(String goodName,Integer pageIndex) {
        log.info("正在查询->goodName:{},pageIndex:{}", goodName,pageIndex);
        IPage<TGood> page = new Page<TGood>(pageIndex,6);
        QueryWrapper<TGood> wrapper = new QueryWrapper<>();
        wrapper.eq("good_name",goodName);
        IPage goodIPage = goodMapper.selectPage(page,wrapper);
        log.info("查询结果->{}", goodIPage);
        if (goodIPage.getTotal()==0L) {
            return Result.fail("数据不存在",null);
        }
        return Result.success("数据存在",goodIPage);
    }

    @Override
    public Result getGoodByGoodType(String goodType, Integer pageIndex) {
        log.info("正在查询->goodType:{},pageIndex:{}", goodType,pageIndex);
        IPage<TGood> page = new Page<TGood>(pageIndex,6);
        QueryWrapper<TGood> wrapper = new QueryWrapper<>();
        wrapper.like("good_type",goodType);
        IPage goodIPage = goodMapper.selectPage(page,wrapper);
        log.info("查询结果->{}", goodIPage);
        if (goodIPage.getTotal()==0L) {
            return Result.fail("数据不存在",null);
        }
        return Result.success("数据存在",goodIPage);
    }

    @Override
    public Result getGoodTypeList() {
        log.info("正在查询->");
        QueryWrapper<TGood> wrapper = new QueryWrapper<>();
        wrapper.groupBy("good_type").select("good_type");
        List typeList = goodMapper.selectList(wrapper);
        log.info("查询结果->{}", typeList);
        if (0==typeList.size()) {
            return Result.fail("数据不存在",null);
        }
        return Result.success("数据存在",typeList);
    }


    @Override
    public Result insertGood(TGood good) {
        log.info("正在添加->good:{}",good);
        int isInsert = goodMapper.insert(good);
        if (1==isInsert) {
            log.info("添加成功->good:{}",good);
            goodService.updateRedis(good.getGoodId());
            return Result.success("添加成功",null);
        }
        log.info("添加失败->");
        return Result.fail("添加失败",null);
    }

    @Override
    public Result updateGoodByGoodId(TGood good) {
        log.info("正在修改->good:{}",good);
        UpdateWrapper<TGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("good_id",good.getGoodId())
                .set(""!=good.getGoodName(),"good_name",good.getGoodName())
                .set(0<=good.getGoodPrice(),"good_price",good.getGoodPrice())
                .set(""!=good.getGoodImage(),"good_image",good.getGoodImage())
                .set(""!=good.getGoodType(),"good_type",good.getGoodType())//类型暂不支持改动
                .set(""!=good.getGoodDetail(),"good_detail",good.getGoodDetail())
                .set(0<=good.getGoodStatus(),"good_status",good.getGoodStatus())
                .set(0<=good.getGoodNum(),"good_num",good.getGoodNum());
        int isUpdate = goodMapper.update(good, wrapper);
        if (1==isUpdate) {
            log.info("修改成功->");
            goodService.updateRedis(good.getGoodId());
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);

    }

    @Override
    public Result updateGoodNumByGoodId(Integer goodId, Integer goodNum) {
        log.info("正在修改->goodId:{},goodNum:{}",goodId,goodNum);
        UpdateWrapper<TGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("good_id",goodId)
                .setSql("good_num=good_num +"+goodNum);
        int isUpdate = goodMapper.update(null, wrapper);
        if (1==isUpdate) {
            log.info("修改成功->");
            goodService.updateRedis(goodId);
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);
    }

    @Override
    public Result updateGoodSellNumByGoodId(Integer goodId, Integer goodSellNum) {
        log.info("正在修改->goodId:{},goodNum:{}",goodId,goodSellNum);
        UpdateWrapper<TGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("good_id",goodId)
                .setSql("good_sell_num=good_sell_num +"+goodSellNum);
        int isUpdate = goodMapper.update(null, wrapper);
        if (1==isUpdate) {
            log.info("修改成功->");
            goodService.updateRedis(goodId);
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);
    }

    @Override
    public Result deleteGoodByGoodId(Integer goodId) {
        log.info("正在删除->goodId:{}",goodId);
        int isDelete = goodMapper.deleteById(goodId);
        if (1==isDelete) {
            log.info("删除成功->");
            goodService.updateRedis(goodId);
            return Result.success("删除成功",null);
        }
        log.info("删除失败->");
        return Result.fail("删除失败",null);
    }
}
